import wx
import wx.lib.filebrowsebutton as fb
import types
#------------------------------------------------------------------------------------
class SelectButton(wx.Panel):
	def __init__(self, parent, id = -1,
					pos = wx.DefaultPosition, size = wx.DefaultSize,
					style = wx.TAB_TRAVERSAL,
					labelText = 'Select a directory:',
					buttonText = 'Browse',
					toolTip = 'Type directory name or browse to select',
					dialogTitle = '',
					startDirectory = '.',
					changeCallback = None,
					dialogClass = wx.DirDialog,
					newDirectory = False,
					name = 'PhaseSelectButton',
					initialValue = "",
					fileMask = "*.*",
					fileMode = wx.FD_OPEN,
					labelWidth = 0,
					hook = None,
					min_choice=3):
		wx.Panel.__init__ (self, parent, id, pos, size, style, name)
		self.dialogClass = dialogClass
		self.newDirectory = newDirectory
		self.hook         = hook
		box = wx.BoxSizer(wx.HORIZONTAL)


		self.labelText = labelText
		self.buttonText = buttonText
		self.toolTip = toolTip
		self.dialogTitle = dialogTitle
		self.startDirectory = startDirectory
		self.initialValue = initialValue
		self.fileMask = fileMask
		self.fileMode = fileMode
		self.changeCallback = changeCallback
		self.callCallback = True
		self.labelWidth = labelWidth


		self.choicelist = ['1', '2', '3', '4', '6', '8', '10', '15', '20', '30','40','50','100','150','200']

		self.label = self.createLabel( )
		box.Add( self.label, 0, wx.CENTER )

		self.textControl = self.createTextControl()
		box.Add( self.textControl, 1, wx.LEFT|wx.CENTER, 5)

		self.browseButton = self.createBrowseButton()
		box.Add( self.browseButton, 0, wx.LEFT|wx.CENTER, 5)

		self.min  = wx.ComboBox(self, -1, value=self.choicelist[min_choice],choices=self.choicelist, style=wx.CB_READONLY)
		self.min.SetToolTipString( "select minimum time that image persist" )
		box.Add( self.min, 0, wx.LEFT|wx.CENTER, 5)

		self.var  = wx.ComboBox(self, -1, value=self.choicelist[1],choices=self.choicelist, style=wx.CB_READONLY)
		self.var.SetToolTipString( "select maximum time variance added to minimum time" )
		box.Add( self.var, 0, wx.LEFT|wx.CENTER, 5)

		# add a border around the whole thing and resize the panel to fit
		outsidebox = wx.BoxSizer(wx.VERTICAL)
		outsidebox.Add(box, 1, wx.EXPAND|wx.ALL, 3)
		outsidebox.Fit(self)

		self.SetAutoLayout(True)
		self.SetSizer( outsidebox )
		self.Layout()

		if type( size ) == types.TupleType:
			size = apply( wx.Size, size)
		self.SetDimensions(-1, -1, size.width, size.height, wx.SIZE_USE_EXISTING)
		self.SetBackgroundColour('DARKGRAY')


	def GetValue (self):
		"""retrieve current value of text control"""
		return self.textControl.GetValue()

	def SetValue (self, value, callBack=1):
		"""set current value of text control"""
		save = self.callCallback
		self.callCallback = callBack
		self.textControl.SetValue(value)
		self.callCallback = save


	def createLabel( self ):
		"""Create the label/caption"""
		label = wx.StaticText(self, -1, self.labelText, style =wx.ALIGN_RIGHT )
		font = label.GetFont()
		w, h, d, e = self.GetFullTextExtent(self.labelText, font)
		if self.labelWidth > 0:
			label.SetSize((self.labelWidth+5, h))
		else:
			label.SetSize((w+5, h))
		return label

	def createTextControl( self):
		"""Create the text control"""
		textControl = wx.TextCtrl(self, -1)
		textControl.SetToolTipString( self.toolTip )
		if self.changeCallback:
			textControl.Bind(wx.EVT_TEXT, self.OnChanged)
			textControl.Bind(wx.EVT_COMBOBOX, self.OnChanged)
		return textControl

	def createBrowseButton( self):
		"""Create the browse-button control"""
		button =wx.Button(self, -1, self.buttonText)
		button.SetToolTipString( self.toolTip )
		button.Bind(wx.EVT_BUTTON, self.OnBrowse)
		return button

	def SetBackgroundColour(self,color):
		wx.Panel.SetBackgroundColour(self,color)
		self.label.SetBackgroundColour(color)


	def OnBrowse(self, evt= None):
		style=0
		if self.hook:
			self.hook(evt)

		if not self.newDirectory:
			style |= wx.DD_DIR_MUST_EXIST

		dialog = self.dialogClass(self,
			message = self.dialogTitle,
			defaultPath = self.startDirectory,
			style = style)

		if dialog.ShowModal() == wx.ID_OK:
			self.SetValue(dialog.GetPath())
		dialog.Destroy()
